package com.halflife.framework.participant.range;

import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class RangeInfoAdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private RangeInfoRepository rangeInfoRepository;

    @RequestMapping("/rangeInfo/list")
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
//        queryParam.put("valid", true);
        queryParam.put("orderSql", "t.ordinal");
        int totalCount = rangeInfoRepository.selectCount(queryParam);
        List<RangeInfo> list = rangeInfoRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        return "rangeInfo/list";
    }

    @RequestMapping("/rangeInfo/operate")
    public String gotoOperate(Integer id,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        RangeInfo rangeInfo = new RangeInfo();
        List<RangeInfo> rangeInfos = new ArrayList<RangeInfo>();
        if(id!=null){
            rangeInfo = rangeInfoRepository.selectByPrimaryKey(id);
        }else{

            //获取最大排序数,并且推送最大值
            Map<String, Object> queryParam_ = new HashMap<>();
            queryParam_.put("valid", true);
            int maxOrdinal = rangeInfoRepository.selectCount(queryParam_);
            rangeInfo.setOrdinal(maxOrdinal + 1);
        }
        //获取父权限列表
        Map<String,Object> queryParam = new HashMap();
        queryParam.put("valid", true);
        queryParam.put("orderSql", "t.ordinal");
        queryParam.put("endSql", "t.rangeInfoId is null");
        rangeInfos = rangeInfoRepository.selectAll(queryParam);
        //推送权限列表
        model.addAttribute("rangeInfos", rangeInfos);
        model.addAttribute("rangeInfo", rangeInfo);
        return "rangeInfo/operate";
    }

    @PostMapping("/rangeInfo/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(RangeInfo rangeInfo, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            if(rangeInfo.getId()!=null){
                rangeInfoRepository.updateByPrimaryKey(rangeInfo);
            }else {
                rangeInfo.init();
                Integer a = rangeInfoRepository.insert(rangeInfo);
            }
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/rangeInfo/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            RangeInfo rangeInfo = new RangeInfo();
            rangeInfoRepository.deleteByPrimaryKey(id);
//            rangeInfo.setId(id);
//            rangeInfo.setValid(Boolean.FALSE);
//            rangeInfoRepository.updateByPrimaryKeySelective(rangeInfo);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
